package com.smbms.controoler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.smbms.pojo.User;
import com.smbms.service.UserService;
import com.smbms.tools.Constrant;

@Controller
@RequestMapping("/manager/backend")
public class UserLoginControoler {
	private Logger logger =Logger.getLogger(UserLoginControoler.class);
	@Autowired
	private UserService userService;
	/**
	 * 跳转到用户登录
	 * @return
	 */
	@RequestMapping(value="/login")
	public String login(){
		return "login";
	}
	/**
	 * 用户登录
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value="dologin")
	public String doLogin(HttpServletRequest request,HttpSession session){
		String userCode=request.getParameter("userCode");
		String userPassword = request.getParameter("userPassword");
		User loginUser=userService.getLoginUser(userCode, userPassword);
		if(loginUser==null){
			logger.error("==>用户名或密码错误");
			return "redirect:login";
		}
		session.setAttribute(Constrant.USER_SESSION,loginUser);
		return "redirect:frame";
	}
	@RequestMapping(value="/frame")
	public String frame(){
		return "frame";
		
	}
	/**
	 * 用户注销
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/logout")
	public String logout(HttpSession session){
		session.removeAttribute(Constrant.USER_SESSION);
		return "login";
		
	}
}
